{
 "cells":[
  {
   "cell_type":"code",
   "source":[
    "import math\n",
    "f = lambda x: x**2-3*x+2-math.e**x\n",
    "fd = lambda x: 2*x-3-math.e**x"
   ],
   "execution_count":1,
   "outputs":[
    
   ],
   "metadata":{
    "datalore":{
     "type":"CODE",
     "hide_input_from_viewers":false,
     "hide_output_from_viewers":false
    }
   }
  },
  {
   "cell_type":"code",
   "source":[
    "#迭代法\n",
    "def b(f, a, b, ep):\n",
    "    fa = f(a)\n",
    "    fb = f(b)\n",
    "\n",
    "    if fa * fb > 0:\n",
    "        return 0\n",
    "    k = 1\n",
    "    while abs(b - a) \/ 2 > ep:\n",
    "        x = (a + b) \/ 2\n",
    "        fx = f(x)\n",
    "        if fx==0:\n",
    "            return 1,k,x\n",
    "        elif fx * fa < 0:\n",
    "            fb = fx\n",
    "            b = x\n",
    "        else:\n",
    "            fa = fx\n",
    "            a = x\n",
    "        k += 1\n",
    "\n",
    "    x = (a + b) \/ 2\n",
    "    return 1, k, x"
   ],
   "execution_count":2,
   "outputs":[
    
   ],
   "metadata":{
    "datalore":{
     "type":"CODE",
     "hide_input_from_viewers":false,
     "hide_output_from_viewers":false
    }
   }
  },
  {
   "cell_type":"code",
   "source":[
    "#递归法\n",
    "def b1(f,a,b,ep,k):\n",
    "    fa = f(a)\n",
    "    fb = f(b)\n",
    "    k=k+1\n",
    "    if fa * fb > 0:\n",
    "        return 0\n",
    "\n",
    "    x = (a + b) \/ 2\n",
    "    fx = f(x)\n",
    "    if(f(x)==0):\n",
    "        return 1,k,x\n",
    "    elif fx * fa < 0:\n",
    "        return b1(f,a,x,ep,k)\n",
    "    elif abs(b - a) \/ 2 < ep:\n",
    "        return 1,k,x\n",
    "    else:\n",
    "        return b1(f,x,b,ep,k)\n",
    "        "
   ],
   "execution_count":3,
   "outputs":[
    
   ],
   "metadata":{
    "datalore":{
     "type":"CODE",
     "hide_input_from_viewers":false,
     "hide_output_from_viewers":false
    }
   }
  },
  {
   "cell_type":"code",
   "source":[
    "def n(f,fd,x,ep,it_max): \n",
    "    index=0 \n",
    "    k=1 \n",
    "    while k<it_max: \n",
    "        x1=x \n",
    "        fv=f(x) \n",
    "        fdv=fd(x) \n",
    "        if abs(fdv)<ep: \n",
    "            break \n",
    "        x=x1-fv\/fdv \n",
    "        if abs(x-x1)<ep: \n",
    "            index=1 \n",
    "            break \n",
    "        k+=1 \n",
    "    return index,k,x"
   ],
   "execution_count":4,
   "outputs":[
    
   ],
   "metadata":{
    "datalore":{
     "type":"CODE",
     "hide_input_from_viewers":false,
     "hide_output_from_viewers":false
    }
   }
  },
  {
   "cell_type":"code",
   "source":[
    "ans_b=b(f,0,1,10**-6)\n",
    "ans_b1=b1(f,0,1,10**-6,0)\n",
    "ans_n=n(f,fd,1.5,10**-6,20)\n",
    "print(ans_b)\n",
    "print(ans_b1)\n",
    "print((ans_n))\n",
    "\n"
   ],
   "execution_count":5,
   "outputs":[
    {
     "name":"stdout",
     "text":[
      "(1, 20, 0.25753116607666016)\n",
      "(1, 24, 0.2575302720069885)\n",
      "(1, 4, 0.257530285439827)\n"
     ],
     "output_type":"stream"
    }
   ],
   "metadata":{
    "datalore":{
     "type":"CODE",
     "hide_input_from_viewers":false,
     "hide_output_from_viewers":false
    }
   }
  }
 ],
 "metadata":{
  "datalore":{
   "version":1,
   "computation_mode":"JUPYTER",
   "package_manager":"pip",
   "base_environment":"default",
   "packages":[
    
   ]
  }
 },
 "nbformat":4,
 "nbformat_minor":4
}